/*!
 * saiche
 * Copyright(c) 2020 huangxin <3203317@qq.com>
 * MIT Licensed
 */
'use strict';

const { Builder, By, Key, until } = require('selenium-webdriver');
const _ = require('underscore');


(() => {
  let _btn_1fsc_start = document.querySelector('#btn_1fsc_start');

  /**
   *
   */
  _btn_1fsc_start.onclick = () => {
    (async function fn(){
      let _tmp = _btn_1fsc_start.innerText;

      _btn_1fsc_start.disabled = true;
      _btn_1fsc_start.innerText = '采集中';

      try{
        let _driver = await new Builder()
                            .forBrowser('chrome')
                            .build();

        {
          await _driver.get('https://jingcai.okooo.com/');
          await sleep(300);
        }

        {
          // await _driver.executeScript('arguments[0].setAttribute("style", "border: 1px solid red");', _v);
          // 删除所有隐藏的div
          // await _driver.executeScript('$("div:hidden").remove();');

          // 设置div层级
          await _driver.executeScript('$("div .cont").css("z-index", 9999);');

          // 滚动到底部
          await _driver.executeScript('window.scrollTo(0, document.body.scrollHeight);');
        }

        {
          // let _vhS = await _driver.findElements(By.css('div .endday .more_bg'));
          // console.log('隐藏的: %d', _vhS.length)

          // let _vS = await _driver.findElements(By.css('div .more_bg'));
          // console.log('全部的: %d', _vS.length);

          // for(let _i=_vhS.length; _i<_vS.length; _i++){
          //   let _ele = _vS[_i];
          //   // 滚动到指定元素
          //   await _driver.executeScript('arguments[0].scrollIntoView(true);', _ele);

          //   await sleep(100);

          //   // 点击指定元素
          //   await _ele.click();
          // }  // end for
        }

        {
          let _script = `let _vS = $("div .more");
                         _vS.each(function(){
                           let _self = $(this);
                           _self.click();
                         });`;

          await _driver.executeScript(_script);

          await sleep(3000);
        }

      }catch(ex){
        console.error(ex);
        alert('采集失败');
        _btn_1fsc_start.disabled = false;
        _btn_1fsc_start.innerText = _tmp;
        return;
      }

      _btn_1fsc_start.disabled = false;
      _btn_1fsc_start.innerText = _tmp;
    })();
  };
})();


let _tmp = document.querySelector('#tmp');

// _btn_1fsc_start.onclick = () => {
//   (async function fn(){
//     _btn_1fsc_start.disabled = true;

//     try{
//       let _driver = await new Builder()
//                           .forBrowser('chrome')
//                           .build();

//       await _driver.get('https://jingcai.okooo.com/');

//       setTimeout(function(){
//         (async function fn(){
//           // await _driver.executeScript('arguments[0].setAttribute("style", "border: 1px solid red");', _v);
//           // 删除所有隐藏的div
//           // await _driver.executeScript('$("div:hidden").remove();');

//           // 设置div层级
//           await _driver.executeScript("$('div .cont').css('z-index', 9999);");

//           let _vhS = await _driver.findElements(By.css('div .endday .more_bg'));
//           console.log('隐藏的: %d', _vhS.length)

//           let _vS = await _driver.findElements(By.css('div .more_bg'));
//           console.log('全部的: %d', _vS.length);

//           for(let _i=_vhS.length; _i<_vS.length; _i++){
//             let _ele = _vS[_i];
//             // 滚动到指定元素
//             await _driver.executeScript('arguments[0].scrollIntoView(true);', _ele);

//             // 此处需要延时执行
//             // 点击指定元素
//             await _ele.click();

//             {
//               let _pEle_1 = _ele.findElement(By.xpath('..')).findElement(By.xpath('..'));

//               {
//                 // let _hname = await _pEle_1.getAttribute('data-hname');
//                 // let _aname = await _pEle_1.getAttribute('data-aname');
//                 // console.log('%s : %s', _hname, _aname);
//               }

//               {
//                 let _pEle_2 = _pEle_1.findElement(By.css('div .shenpf'));

//                 let _pEle_2_zhu = _pEle_2.findElement(By.css('div .zhu .zhum'));
//                 let _pEle_2_fu = _pEle_2.findElement(By.css('div .fu .zhum'));

//                 let _hname = await _pEle_2_zhu.getAttribute('title');
//                 let _aname = await _pEle_2_fu.getAttribute('title');
//                 console.log('%s : %s', _hname, _aname);
//               }
//             }
//           }  // end for
//         })();

//         // 延时获取更多
//         setTimeout(function(){
//           (async function fn(){
//             let _vS = await _driver.findElements(By.css('div .more_zk .mrfg .float_l .zk_1'));
//             console.log('更多的: %d', _vS.length);

//             for(let _ele of _vS){
//               let _p = _ele.getAttribute('innerHTML');
//               let _v = await _p;

//               // console.log(_v);
//               _tmp.innerHTML = _v;
//             }  // end for

//             {
//               // await _driver.quit();
//               _btn_1fsc_start.disabled = false;
//             }
//           })();
//         }, 5000);

//       }, 10);

//       // setInterval(function(){
//       //   console.log(new Date());
//       // }, 1000);

//     }catch(ex){
//       alert('采集失败');
//     }finally{
//       // 
//     }
//   })();
// };  // end onclick


const request = require('request');
const iconv = require('iconv-lite');
const cheerio = require('cheerio');

(() => {
  let _btn_2fsc_start = document.querySelector('#btn_2fsc_start');

  let _opt = {
    method: 'GET',
    uri: 'https://trade.500.com/jczq/',
    timeout: 1000 * 3,
    encoding: null,
  };

  /**
   *
   */
  _btn_2fsc_start.onclick = () => {
    (async function fn(){
      let _tmp = _btn_2fsc_start.innerText;

      _btn_2fsc_start.disabled = true;
      _btn_2fsc_start.innerText = '采集中';

      // console.log(1);

      // await sleep(3000);

      // console.log(2);

      try{
        let _data = await rpn(_opt);

        let _buf = iconv.decode(_data, 'gb2312');
        // console.log(_buf);

        {
          let $ = cheerio.load(_buf, { decodeEntities: false, });

          let _vS = $('div.bet-main').find('a:contains("析")');

          _vS.each(function (i /* 0开始 */, ele /* 元素 */){
            let _self = $(this);
            console.log(_self);
          });  // for
        }
      }catch(ex){
        console.error(ex);
        alert('采集失败');
        _btn_2fsc_start.disabled = false;
        _btn_2fsc_start.innerText = _tmp;
        return;
      }

      // await sleep(3000);

      // console.log(3);

      _btn_2fsc_start.disabled = false;
      _btn_2fsc_start.innerText = _tmp;
    })();
  };
})();


const rpn = require('request-promise-native');

(() => {
  let _btn_3fsc_start = document.querySelector('#btn_3fsc_start');

  let _opt = {
    method: 'GET',
    uri: 'https://jingcai.okooo.com/',
    timeout: 1000 * 3,
    encoding: null,
  };

  /**
   *
   */
  _btn_3fsc_start.onclick = () => {
    (async function fn(){
      _btn_3fsc_start.disabled = true;

      console.log(1);

      await sleep(3000);

      console.log(2);

      try{
        let _data = await rpn(_opt);

        let _buf = iconv.decode(_data, 'gb2312');
        // console.log(_buf);

        {
          let $ = cheerio.load(_buf, { decodeEntities: false, });

          let _vhS = $('div .endday .more_bg');
          // console.log(_vhS.length);

          let _vS = $('div .touzhu_1');
          // console.log(_vS);

          for(let _i=_vhS.length; _i<_vS.length; _i++){
            let _v = $(_vS[_i]);

            let _p_morder = _v.attr('data-morder');
            console.log(_p_morder);
          }  // for
        }
      }catch(ex){
        console.error(ex);
        alert('采集失败');
        _btn_3fsc_start.disabled = false;
        return;
      }

      await sleep(3000);

      console.log(3);

      _btn_3fsc_start.disabled = false;
    })();
  };
})();


(() => {
  function login(str){
    return new Promise(function(resolve, reject){
      // console.log('业务逻辑');
      // resolve(str);
      // throw new Error('抛异常');
      reject('逻辑错误');
    });
  }

  let _btn_4fsc_start = document.querySelector('#btn_4fsc_start');

  /**
   *
   */
  _btn_4fsc_start.onclick = () => {
    (async function fn(){
      _btn_4fsc_start.disabled = true;

      console.log(1);

      await sleep(3000);

      console.log(2);

      try{
        let _v = await login('登录');
        console.log(_v);
      }catch(ex){
        console.error(ex);
        _btn_4fsc_start.disabled = false;
        return;
      }

      await sleep(3000);

      console.log(3);

      _btn_4fsc_start.disabled = false;
    })();
  };
})();

/**
 *
 */
function sleep(ms){
  return new Promise(function(resolve, reject){
    setTimeout(resolve, ms);
  });
}
